#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QProcess>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    timer1 = new QTimer();

    connect(timer1, SIGNAL(timeout()), this, SLOT(onTimer()));

    if ((fd1 = open(DEV1,O_RDWR)) < 0)
    {
        printf("Failed to open the bus %s\n",DEV1);
    }
    if ((fd2 = open(DEV2,O_RDWR)) < 0)
    {
        printf("Failed to open the bus %s\n",DEV2);
    }

    timer1->start(2000);
    thread = new read_thread();
    thread->start();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::onTimer()
{
    am2321 data = am2321_read(fd1);
    ui->lcdNumber_h->display(data.humi);
    ui->lcdNumber_t->display(data.temp);
    QString str1 = "/usr/bin/mosquitto_pub -t arm_h -m ";
    QString str2 = "/usr/bin/mosquitto_pub -t arm_t -m ";
    str1.append(QString::number(data.humi));
    str2.append(QString::number(data.temp));
    QProcess::execute(str1);
    QProcess::execute(str2);

    Time t = ds1307_readtime(fd2);
    str1 = "/usr/bin/mosquitto_pub -t arm_d -m ";
    str1.append(QString::number(t.hou)+":"+QString::number(t.min)+":"+QString::number(t.sec));
    QProcess::execute(str1);


}

void MainWindow::am2321_wake(int fd)
{
    char buf_write = 0x00;
    int addr = AM2321_ADDR;
    // am2321 i2c slave address
    if (ioctl(fd, I2C_SLAVE, addr) < 0) {
        printf("Failed to acquire bus access and/or talk to slave.\n");
    }
    write(fd,&buf_write,1);
}

am2321 MainWindow::am2321_read(int fd)
{
    am2321 data;
    char buf_write[3];
    char buf_read[6];
    int addr = AM2321_ADDR;

    am2321_wake(fd1);
    // am2321 i2c slave address
    if (ioctl(fd,I2C_SLAVE,addr) < 0) {
        printf("Failed to acquire bus access and/or talk to slave.\n");
    }

    buf_write[0] = 0x03;  //function code 0x03 ,means read sensor register
    buf_write[1] = 0x00;  //start address 0x00 ,0x00 is humidity HIGH
    buf_write[2] = 0x04;  //read 4 registers,from 0x00 to 0x04
    write(fd,buf_write,3);

    read(fd,buf_read,6);

    int tmp = buf_read[2];
    tmp = (tmp << 8) | buf_read[3];
    data.humi = ((float)tmp / 10);

    tmp = buf_read[4];
    tmp = (tmp << 8) | buf_read[5];
    data.temp = ((float)tmp / 10);

    return data;
}

uint8_t MainWindow::bcd2dec(uint8_t val)
{
    return ( (val/16*10) + (val%16) );
}

Time MainWindow::ds1307_readtime(int fd)
{
    Time t;
    char buf_write[1];
    char buf_read[7];
    int addr = DS1307_ADDR;
    // ds1302 i2c slave address
    if (ioctl(fd,I2C_SLAVE,addr) < 0) {
        printf("Failed to acquire bus access and/or talk to slave.\n");
    }
    // ds1302 register address
    buf_write[0] = 0x00;
    write(fd,buf_write,1);
    read(fd,buf_read,7);

    t.sec = bcd2dec(buf_read[0] & 0x7f);
    t.min = bcd2dec(buf_read[1]);
    t.hou = bcd2dec(buf_read[2] & 0x3f);
    t.dayOfWeek = bcd2dec(buf_read[3]);
    t.dayOfMonth = bcd2dec(buf_read[4]);
    t.month = bcd2dec(buf_read[5]);
    t.year  = bcd2dec(buf_read[6]);

    return t;
}

